const {ipcMain, dialog} = require('electron')
module.exports = (app,mainWindow)=>{
    ipcMain.handle('select-directory',(event,arg)=>{
        const that = this;
        const { dialog } = require('electron')
        const dir_arr=dialog.showOpenDialogSync(that.mainWindow, {
            properties: ['openDirectory']
        })
        return dir_arr[0];
    });
    ipcMain.handle('select-file',(event,arg)=>{
        const that = this;
        const { dialog } = require('electron')
        const file_arr=dialog.showOpenDialogSync(that.mainWindow, {
            properties: ['openFile']
        })
        return file_arr[0];
    });
    ipcMain.handle('save-file',(event,arg)=>{
        const that = this;
        const { dialog } = require('electron')
        const file_arr=dialog.showSaveDialogSync(that.mainWindow, {
            properties: ['openFile']
        })
        return file_arr[0];
    });
    ipcMain.handle('show-message-box',(event,arg)=>{
        const that = this;
        const { dialog } = require('electron')
        const options = {
            type: 'info',
            buttons: ['OK'],
            title: 'Message',
            message: arg.message
        }
        dialog.showMessageBoxSync(that.mainWindow, options)
    });
    ipcMain.handle('show-error-box',(event,arg)=>{
        const that = this;
        const { dialog } = require('electron')
        const options = {
            type: 'error',
            buttons: ['OK'],
            title: 'Error',
            message: arg.message
        }
        dialog.showMessageBoxSync(that.mainWindow, options)
    });
    ipcMain.handle('get-directory-images',(event,arg)=>{
        const that = this;
        const { dialog } = require('electron')
        const dir_arr=dialog.showOpenDialogSync(that.mainWindow, {
            properties: ['openDirectory'],
            filters: [
                { name: 'Images', extensions: ['jpg', 'png', 'gif'] }
            ]
        })
        const images_arr = [];
        const image_dir = dir_arr[0];
        if(image_dir){
            const fs = require('fs');
            const files = fs.readdirSync(image_dir);
            files.forEach(file => {
                const ext = file.split('.').pop();
                if(['jpg', 'png', 'gif'].includes(ext)){
                    images_arr.push({
                        name: file,
                        path: `${image_dir}/${file}`
                    });
                }
            });
        }
        return images_arr;
    });
    ipcMain.handle('get-image-json',(event,arg)=>{
        const that = this;
        const fs = require('fs');
        const path = require('path');
        const image_path = arg.path;
        const image_name = path.basename(image_path);
        const image_json_path = path.join(path.dirname(image_path), `${image_name.split('.')[0]}.json`);
        if(fs.existsSync(image_json_path)) {
            let image_json = null;
            const image_json_str = fs.readFileSync(image_json_path, 'utf8');
            if (image_json_str) {
                try {
                    image_json = JSON.parse(image_json_str);
                } catch (e) {
                    console.error(e);
                }
            }
            return image_json;
        }else {
            return null;
        }
    });
    ipcMain.handle('save-image-json',(event,arg)=>{
        const that = this;
        const fs = require('fs');
        const path = require('path');
        const image_path = arg.path;
        const image_name = path.basename(image_path);
        const image_json_path = path.join(path.dirname(image_path), `${image_name.split('.')[0]}.json`);
        const image_json_str = JSON.stringify(arg.json);
        fs.writeFileSync(image_json_path, image_json_str);
        return true;
    });
}